#include "StarPollUtil.h"
#include "entity/Entity/Star/Star.h"
#include "global/GlobalPath.h"
#include "global/GlobalDefine.h"
#include "global/GlobalParam.h"
#include "cocos-ext.h"

USING_NS_CC_EXT;
using namespace cocos2d::ui;

StarPollUtil* StarPollUtil::m_starPollUtil = NULL;

StarPollUtil::StarPollUtil(){
	m_starNum = GlobalParam::InitStarPoolSize;
	m_allStar = NULL;
}

StarPollUtil::~StarPollUtil(){
	while (!m_queue.empty()){
		m_queue.pop();
	}
	CC_SAFE_RELEASE(m_allStar);
}

StarPollUtil* StarPollUtil::shareStarPollUtil(){
	if(m_starPollUtil == NULL){
		m_starPollUtil = StarPollUtil::create();
		CCLOG("StarPollUtil create");
	}
	return m_starPollUtil;
}

bool StarPollUtil::init(){
	bool bRet = false;
	do{
		m_allStar = CCArray::create();
		CC_SAFE_RETAIN(m_allStar);

		for (int i = 0; i < m_starNum; i++){
			Star* star = Star::create();
			CCSprite* sprite = CCSprite::create(PATH_PNG_SPRITE_star_main);
			star->bindSprite(sprite);
			star->bindSprite(CCArmature::create("StarAnimation"));

			m_allStar->addObject(star);
			pushStarAndStop(star);
		}
		bRet = true;
	}while(0);
	return bRet;
}

Star* StarPollUtil::popStar(){
	Star* star = m_queue.front();
	m_queue.pop();
	return star;
}

void StarPollUtil::pushStar(Star* star){
	m_queue.push(star);
}

Star* StarPollUtil::popStarAndBegin(){
	Star* star = popStar();
	
	star->getSprite()->setVisible(true);
	star->getArmature()->setVisible(true);
	star->getArmature()->getAnimation()->playByIndex(0); 
	return star;
}

void StarPollUtil::pushStarAndStop(Star* star){
	star->setPosition(ccp(-50,-50));
	star->getSprite()->setVisible(false);
	star->getArmature()->setVisible(false);
	star->getArmature()->getAnimation()->stop();
	pushStar(star);
	CCLOG("%d",m_queue.size());
}